//
//  PulldownView.m
//  BaseFrame
//
//  Created by wingfac on 2017/8/11.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "PulldownView.h"
#import "PulldownCell.h"
@interface PulldownView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UIView *bgView; // 背景view
@property (nonatomic,strong) UITableView *tableView;    // 下拉菜单

@end
@implementation PulldownView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 背景
        self.backgroundColor = [UIColor clearColor];
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_HEIGHT - 64 -49)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [self.bgView addGestureRecognizer:tap];
        [self addSubview:_bgView];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3, 0, SCREEN_WIDTH/3, SCREEN_HEIGHT/2)];
        _tableView.backgroundColor = getColor(mainColor);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return self;
}
#pragma mark - UITableViewDelegate & UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 45 / WIDTH_5S_SCALE;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PulldownCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PulldownCell class])];
    if(!cell){
        cell = [[PulldownCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([PulldownCell class])];
    }
    [cell setType:self.dataArray[indexPath.row][@"cc_name"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self hide];
    if(self.clickedAtIndexBlock){
        self.clickedAtIndexBlock(indexPath.row);
    }
}

#pragma mark -- show
- (void)showInSuperView:(UIView *)view {
    
    [view addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
- (void)hide{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark - data

- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    [self addSubview:self.tableView];
}
@end
